home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / frame.el < prev    next >
Lisp/Scheme  |  1993-07-03  |  20KB  |  518 lines

  1. ;;; frame.el --- multi-frame management independent of window systems.
  2.  
  3. ;;;; Copyright (C) 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: internal
  7.  
  8. ;;; This file is part of GNU Emacs.
  9. ;;;
  10. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;;; it under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 2, or (at your option)
  13. ;;; any later version.
  14. ;;;
  15. ;;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Code:
  25.  
  26. (defvar frame-creation-function nil
  27.   "Window-system dependent function to call to create a new frame.
  28. The window system startup file should set this to its frame creation
  29. function, which should take an alist of parameters as its argument.")
  30.  
  31. ;;; The initial value given here for this must ask for a minibuffer.
  32. ;;; There must always exist a frame with a minibuffer, and after we
  33. ;;; delete the terminal frame, this will be the only frame.
  34. (defvar initial-frame-alist '((minibuffer . t))
  35.   "Alist of frame parameters for creating the initial X window frame.
  36. You can set this in your `.emacs' file; for example,
  37.  (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
  38. If the value calls for a frame without a minibuffer, and you do not create a
  39. minibuffer frame on your own, one is created according to
  40. `minibuffer-frame-alist'.
  41. Parameters specified here supersede the values given in
  42. `default-frame-alist'.")
  43.  
  44. (defvar minibuffer-frame-alist '((width . 80) (height . 2))
  45.   "Alist of frame parameters for initially creating a minibuffer frame.
  46. You can set this in your `.emacs' file; for example,
  47.  (setq minibuffer-frame-alist
  48.    '((top . 1) (left . 1) (width . 80) (height . 2)))
  49. Parameters specified here supersede the values given in
  50. `default-frame-alist'.")
  51.  
  52. (defvar pop-up-frame-alist nil
  53.   "Alist of frame parameters used when creating pop-up frames.
  54. Pop-up frames are used for completions, help, and the like.
  55. This variable can be set in your init file, like this:
  56.   (setq pop-up-frame-alist '((width . 80) (height . 20)))
  57. These supersede the values given in `default-frame-alist'.")
  58.  
  59. (setq pop-up-frame-function
  60.       (function (lambda ()
  61.           (new-frame pop-up-frame-alist))))
  62.  
  63.  
  64. ;;;; Arrangement of frames at startup
  65.  
  66. ;;; 1) Load the window system startup file from the lisp library and read the
  67. ;;; high-priority arguments (-q and the like).  The window system startup
  68. ;;; file should create any frames specified in the window system defaults.
  69. ;;; 
  70. ;;; 2) If no frames have been opened, we open an initial text frame.
  71. ;;;
  72. ;;; 3) Once the init file is done, we apply any newly set parameters
  73. ;;; in initial-frame-alist to the frame.
  74.  
  75. ;; These are now called explicitly at the proper times, 
  76. ;; since that is easier to understand.
  77. ;; Actually using hooks within Emacs is bad for future maintenance. --rms.
  78. ;; (add-hook 'before-init-hook 'frame-initialize)
  79. ;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
  80.  
  81. ;;; If we create the initial frame, this is it.
  82. (defvar frame-initial-frame nil)
  83.  
  84. ;;; startup.el calls this function before loading the user's init
  85. ;;; file - if there is no frame with a minibuffer open now, create
  86. ;;; one to display messages while loading the init file.
  87. (defun frame-initialize ()
  88.   
  89.   ;; Are we actually running under a window system at all?
  90.   (if (and window-system (not noninteractive))
  91.       (progn
  92.     ;; If there is no frame with a minibuffer besides the terminal
  93.     ;; frame, then we need to create the opening frame.  Make sure
  94.     ;; it has a minibuffer, but let initial-frame-alist omit the
  95.     ;; minibuffer spec.
  96.     (or (delq terminal-frame (minibuffer-frame-list))
  97.         (progn
  98.           (setq default-minibuffer-frame
  99.             (setq frame-initial-frame
  100.               (new-frame initial-frame-alist)))
  101.           ;; Delete any specifications for window geometry parameters
  102.           ;; so that we won't reapply them in frame-notice-user-settings.
  103.           ;; It would be wrong to reapply them then,
  104.           ;; because that would override explicit user resizing.
  105.           (setq initial-frame-alist
  106.             (frame-remove-geometry-params initial-frame-alist))
  107.           ;; Handle `reverse' as a parameter.
  108.           (if (cdr (or (assq 'reverse initial-frame-alist)
  109.                (assq 'reverse default-frame-alist)))
  110.           (let ((params (frame-parameters frame-initial-frame)))
  111.             (modify-frame-parameters
  112.              frame-initial-frame
  113.              ;; Must set cursor-color after background color.
  114.              ;; So put it first.
  115.              (list (cons 'cursor-color
  116.                  (cdr (assq 'background-color params)))
  117.                (cons 'foreground-color
  118.                  (cdr (assq 'background-color params)))
  119.                (cons 'background-color
  120.                  (cdr (assq 'foreground-color params)))
  121.                (cons 'mouse-color
  122.                  (cdr (assq 'background-color params)))
  123.                (cons 'border-color
  124.                  (cdr (assq 'background-color params)))))))))
  125.  
  126.     ;; At this point, we know that we have a frame open, so we 
  127.     ;; can delete the terminal frame.
  128.     (delete-frame terminal-frame)
  129.     (setq terminal-frame nil))
  130.     
  131.     ;; No, we're not running a window system.  Arrange to cause errors.
  132.     (setq frame-creation-function
  133.       (function
  134.        (lambda (parameters)
  135.          (error
  136.           "Can't create multiple frames without a window system"))))))
  137.                     
  138. ;;; startup.el calls this function after loading the user's init
  139. ;;; file.  Now default-frame-alist and initial-frame-alist contain
  140. ;;; information to which we must react; do what needs to be done.
  141. (defun frame-notice-user-settings ()
  142.  
  143.   ;; Creating and deleting frames may shift the selected frame around,
  144.   ;; and thus the current buffer.  Protect against that.  We don't
  145.   ;; want to use save-excursion here, because that may also try to set
  146.   ;; the buffer of the selected window, which fails when the selected
  147.   ;; window is the minibuffer.
  148.   (let ((old-buffer (current-buffer)))
  149.  
  150.     ;; If the initial frame is still around, apply initial-frame-alist
  151.     ;; and default-frame-alist to it.
  152.     (if (frame-live-p frame-initial-frame)
  153.  
  154.     ;; The initial frame we create above always has a minibuffer.
  155.     ;; If the user wants to remove it, or make it a minibuffer-only
  156.     ;; frame, then we'll have to delete the current frame and make a
  157.     ;; new one; you can't remove or add a root window to/from an
  158.     ;; existing frame.
  159.     ;;
  160.     ;; NOTE: default-frame-alist was nil when we created the
  161.     ;; existing frame.  We need to explicitly include
  162.     ;; default-frame-alist in the parameters of the screen we
  163.     ;; create here, so that its new value, gleaned from the user's
  164.     ;; .emacs file, will be applied to the existing screen.
  165.     (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
  166.                   (assq 'minibuffer default-frame-alist)
  167.                   '(minibuffer . t)))
  168.              t))
  169.         ;; Create the new frame.
  170.         (let ((new
  171.            (new-frame
  172.             (append initial-frame-alist
  173.                 default-frame-alist
  174.                 (frame-parameters frame-initial-frame)))))
  175.  
  176.           ;; The initial frame, which we are about to delete, may be
  177.           ;; the only frame with a minibuffer.  If it is, create a
  178.           ;; new one.
  179.           (or (delq frame-initial-frame (minibuffer-frame-list))
  180.           (new-frame (append minibuffer-frame-alist
  181.                      '((minibuffer . only)))))
  182.  
  183.           ;; If the initial frame is serving as a surrogate
  184.           ;; minibuffer frame for any frames, we need to wean them
  185.           ;; onto a new frame.  The default-minibuffer-frame
  186.           ;; variable must be handled similarly.
  187.           (let ((users-of-initial
  188.              (filtered-frame-list
  189.               (function (lambda (frame)
  190.                   (and (not (eq frame frame-initial-frame))
  191.                        (eq (window-frame
  192.                         (minibuffer-window frame))
  193.                        frame-initial-frame)))))))
  194.         (if (or users-of-initial
  195.             (eq default-minibuffer-frame frame-initial-frame))
  196.  
  197.             ;; Choose an appropriate frame.  Prefer frames which
  198.             ;; are only minibuffers.
  199.             (let* ((new-surrogate
  200.                 (car
  201.                  (or (filtered-frame-list
  202.                   (function
  203.                    (lambda (frame)
  204.                      (eq (cdr (assq 'minibuffer
  205.                             (frame-parameters frame)))
  206.                      'only))))
  207.                  (minibuffer-frame-list))))
  208.                (new-minibuffer (minibuffer-window new-surrogate)))
  209.  
  210.               (if (eq default-minibuffer-frame frame-initial-frame)
  211.               (setq default-minibuffer-frame new-surrogate))
  212.  
  213.               ;; Wean the frames using frame-initial-frame as
  214.               ;; their minibuffer frame.
  215.               (mapcar
  216.                (function
  217.             (lambda (frame)
  218.               (modify-frame-parameters
  219.                frame (list (cons 'minibuffer new-minibuffer)))))
  220.                users-of-initial))))
  221.  
  222.           ;; Redirect events enqueued at this frame to the new frame.
  223.           ;; Is this a good idea?
  224.           (redirect-frame-focus frame-initial-frame new)
  225.  
  226.           ;; Finally, get rid of the old frame.
  227.           (delete-frame frame-initial-frame))
  228.  
  229.       ;; Otherwise, we don't need all that rigamarole; just apply
  230.       ;; the new parameters.
  231.       (modify-frame-parameters frame-initial-frame
  232.                    (append initial-frame-alist
  233.                        default-frame-alist))))
  234.  
  235.     ;; Restore the original buffer.
  236.     (set-buffer old-buffer)
  237.  
  238.     ;; Make sure the initial frame can be GC'd if it is ever deleted.
  239.     ;; Make sure frame-notice-user-settings does nothing if called twice.
  240.     (setq frame-initial-frame nil)))
  241.  
  242.  
  243. ;;;; Creation of additional frames, and other frame miscellanea
  244.  
  245. ;;; Return some frame other than the current frame, creating one if
  246. ;;; necessary.  Note that the minibuffer frame, if separate, is not
  247. ;;; considered (see next-frame).
  248. (defun get-other-frame ()
  249.   (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
  250.            (new-frame)
  251.          (next-frame (selected-frame)))))
  252.     s))
  253.  
  254. (defun next-multiframe-window ()
  255.   "Select the next window, regardless of which frame it is on."
  256.   (interactive)
  257.   (select-window (next-window (selected-window)
  258.                   (> (minibuffer-depth) 0)
  259.                   t)))
  260.  
  261. (defun previous-multiframe-window ()
  262.   "Select the previous window, regardless of which frame it is on."
  263.   (interactive)
  264.   (select-window (previous-window (selected-window)
  265.                   (> (minibuffer-depth) 0)
  266.                   t)))
  267.  
  268. ;; Alias, kept temporarily.
  269. (defalias 'new-frame 'make-frame)
  270. (defun make-frame (&optional parameters)
  271.   "Create a new frame, displaying the current buffer.
  272.  
  273. Optional argument PARAMETERS is an alist of parameters for the new
  274. frame.  Specifically, PARAMETERS is a list of pairs, each having one
  275. of the following forms:
  276.  
  277. \(name . STRING)    - The frame should be named STRING.
  278.  
  279. \(height . NUMBER) - The frame should be NUMBER text lines high.  If
  280.     this parameter is present, the width parameter must also be
  281.     given.
  282.  
  283. \(width . NUMBER) - The frame should be NUMBER characters in width.
  284.     If this parameter is present, the height parameter must also
  285.     be given.
  286.  
  287. \(minibuffer . t) - the frame should have a minibuffer
  288. \(minibuffer . nil) - the frame should have no minibuffer
  289. \(minibuffer . only) - the frame should contain only a minibuffer
  290. \(minibuffer . WINDOW) - the frame should use WINDOW as its minibuffer window.
  291.  
  292. The documentation for the function `x-create-frame' describes
  293. additional frame parameters that Emacs recognizes for X window frames."
  294.   (interactive)
  295.   (funcall frame-creation-function parameters))
  296.  
  297. (defun filtered-frame-list (predicate)
  298.   "Return a list of all live frames which satisfy PREDICATE."
  299.   (let ((frames (frame-list))
  300.     good-frames)
  301.     (while (consp frames)
  302.       (if (funcall predicate (car frames))
  303.       (setq good-frames (cons (car frames) good-frames)))
  304.       (setq frames (cdr frames)))
  305.     good-frames))
  306.  
  307. (defun minibuffer-frame-list ()
  308.   "Return a list of all frames with their own minibuffers."
  309.   (filtered-frame-list
  310.    (function (lambda (frame)
  311.            (eq frame (window-frame (minibuffer-window frame)))))))
  312.  
  313. (defun frame-remove-geometry-params (param-list)
  314.   "Return the parameter list PARAM-LIST, but with geometry specs removed.
  315. This deletes all bindings in PARAM-LIST for `top', `left', `width',
  316. and `height' parameters.
  317. Emacs uses this to avoid overriding explicit moves and resizings from
  318. the user during startup."
  319.   (setq param-list (cons nil param-list))
  320.   (let ((tail param-list))
  321.     (while (consp (cdr tail))
  322.       (if (and (consp (car (cdr tail)))
  323.            (memq (car (car (cdr tail))) '(height width top left)))
  324.       (setcdr tail (cdr (cdr tail)))
  325.     (setq tail (cdr tail)))))
  326.   (cdr param-list))
  327.  
  328.  
  329.  
  330. ;;;; Frame configurations
  331.  
  332. (defun current-frame-configuration ()
  333.   "Return a list describing the positions and states of all frames.
  334. Its car is `frame-configuration'.
  335. Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
  336. where
  337.   FRAME is a frame object,
  338.   ALIST is an association list specifying some of FRAME's parameters, and
  339.   WINDOW-CONFIG is a window configuration object for FRAME."
  340.   (cons 'frame-configuration
  341.     (mapcar (function
  342.          (lambda (frame)
  343.            (list frame
  344.              (frame-parameters frame)
  345.              (current-window-configuration frame))))
  346.         (frame-list))))
  347.  
  348. (defun set-frame-configuration (configuration)
  349.   "Restore the frames to the state described by CONFIGURATION.
  350. Each frame listed in CONFIGURATION has its position, size, window
  351. configuration, and other parameters set as specified in CONFIGURATION."
  352.   (or (frame-configuration-p configuration)
  353.       (signal 'wrong-type-argument
  354.           (list 'frame-configuration-p configuration)))
  355.   (let ((config-alist (cdr configuration))
  356.     frames-to-delete)
  357.     (mapcar (function
  358.          (lambda (frame)
  359.            (let ((parameters (assq frame config-alist)))
  360.          (if parameters
  361.              (progn
  362.                (modify-frame-parameters
  363.             frame
  364.             ;; Since we can't set a frame's minibuffer status, 
  365.             ;; we might as well omit the parameter altogether.
  366.             (let* ((parms (nth 1 parameters))
  367.                    (mini (assq 'minibuffer parms)))
  368.               (if mini (setq parms (delq mini parms)))
  369.               parms))
  370.                (set-window-configuration (nth 2 parameters)))
  371.            (setq frames-to-delete (cons frame frames-to-delete))))))
  372.         (frame-list))
  373.     (mapcar 'delete-frame frames-to-delete)))
  374.  
  375. (defun frame-configuration-p (object)
  376.   "Return non-nil if OBJECT seems to be a frame configuration.
  377. Any list whose car is `frame-configuration' is assumed to be a frame
  378. configuration."
  379.   (and (consp object)
  380.        (eq (car object) 'frame-configuration)))
  381.  
  382.  
  383. ;;;; Convenience functions for accessing and interactively changing
  384. ;;;; frame parameters.
  385.  
  386. (defun frame-height (&optional frame)
  387.   "Return number of lines available for display on FRAME.
  388. If FRAME is omitted, describe the currently selected frame."
  389.   (cdr (assq 'height (frame-parameters frame))))
  390.  
  391. (defun frame-width (&optional frame)
  392.   "Return number of columns available for display on FRAME.
  393. If FRAME is omitted, describe the currently selected frame."
  394.   (cdr (assq 'width (frame-parameters frame))))
  395.  
  396. (defun set-default-font (font-name)
  397.   "Set the font of the selected frame to FONT.
  398. When called interactively, prompt for the name of the font to use."
  399.   (interactive "sFont name: ")
  400.   (modify-frame-parameters (selected-frame)
  401.                (list (cons 'font font-name))))
  402.  
  403. (defun set-background-color (color-name)
  404.   "Set the background color of the selected frame to COLOR.
  405. When called interactively, prompt for the name of the color to use."
  406.   (interactive "sColor: ")
  407.   (modify-frame-parameters (selected-frame)
  408.                (list (cons 'background-color color-name))))
  409.  
  410. (defun set-foreground-color (color-name)
  411.   "Set the foreground color of the selected frame to COLOR.
  412. When called interactively, prompt for the name of the color to use."
  413.   (interactive "sColor: ")
  414.   (modify-frame-parameters (selected-frame)
  415.                (list (cons 'foreground-color color-name))))
  416.  
  417. (defun set-cursor-color (color-name)
  418.   "Set the text cursor color of the selected frame to COLOR.
  419. When called interactively, prompt for the name of the color to use."
  420.   (interactive "sColor: ")
  421.   (modify-frame-parameters (selected-frame)
  422.                (list (cons 'cursor-color color-name))))
  423.  
  424. (defun set-mouse-color (color-name)
  425.   "Set the color of the mouse pointer of the selected frame to COLOR.
  426. When called interactively, prompt for the name of the color to use."
  427.   (interactive "sColor: ")
  428.   (modify-frame-parameters (selected-frame)
  429.                (list (cons 'mouse-color color-name))))
  430.  
  431. (defun set-border-color (color-name)
  432.   "Set the color of the border of the selected frame to COLOR.
  433. When called interactively, prompt for the name of the color to use."
  434.   (interactive "sColor: ")
  435.   (modify-frame-parameters (selected-frame)
  436.                (list (cons 'border-color color-name))))
  437.  
  438. (defun auto-raise-mode (arg)
  439.   "Toggle whether or not the selected frame should auto-raise.
  440. With arg, turn auto-raise mode on if and only if arg is positive."
  441.   (interactive "P")
  442.   (if (null arg)
  443.       (setq arg
  444.         (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
  445.         -1 1)))
  446.   (modify-frame-parameters (selected-frame)
  447.                (list (cons 'auto-raise (> arg 0)))))
  448.  
  449. (defun auto-lower-mode (arg)
  450.   "Toggle whether or not the selected frame should auto-lower.
  451. With arg, turn auto-lower mode on if and only if arg is positive."
  452.   (interactive "P")
  453.   (if (null arg)
  454.       (setq arg
  455.         (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
  456.         -1 1)))
  457.   (modify-frame-parameters (selected-frame)
  458.                (list (cons 'auto-lower (> arg 0)))))
  459.  
  460. (defun toggle-scroll-bar (arg)
  461.   "Toggle whether or not the selected frame has vertical scroll bars.
  462. With arg, turn vertical scroll bars on if and only if arg is positive."
  463.   (interactive "P")
  464.   (if (null arg)
  465.       (setq arg
  466.         (if (cdr (assq 'vertical-scroll-bars
  467.                (frame-parameters (selected-frame))))
  468.         -1 1)))
  469.   (modify-frame-parameters (selected-frame)
  470.                (list (cons 'vertical-scroll-bars (> arg 0)))))
  471.  
  472. (defun toggle-horizontal-scroll-bar (arg)
  473.   "Toggle whether or not the selected frame has horizontal scroll bars.
  474. With arg, turn horizontal scroll bars on if and only if arg is positive.
  475. Horizontal scroll bars aren't implemented yet."
  476.   (interactive "P")
  477.   (error "Horizontal scroll bars aren't implemented yet"))
  478.  
  479.  
  480. ;;;; Aliases for backward compatibility with Emacs 18.
  481. (defalias 'screen-height 'frame-height)
  482. (defalias 'screen-width 'frame-width)
  483.  
  484. (defun set-screen-width (cols &optional pretend)
  485.   "Obsolete function to change the size of the screen to COLS columns.\n\
  486. Optional second arg non-nil means that redisplay should use COLS columns\n\
  487. but that the idea of the actual width of the frame should not be changed.\n\
  488. This function is provided only for compatibility with Emacs 18; new code\n\
  489. should use `set-frame-width instead'."
  490.   (set-frame-width (selected-frame) cols pretend))
  491.  
  492. (defun set-screen-height (lines &optional pretend)
  493.   "Obsolete function to change the height of the screen to LINES lines.\n\
  494. Optional second arg non-nil means that redisplay should use LINES lines\n\
  495. but that the idea of the actual height of the screen should not be changed.\n\
  496. This function is provided only for compatibility with Emacs 18; new code\n\
  497. should use `set-frame-width' instead."
  498.   (set-frame-height (selected-frame) lines pretend))
  499.  
  500. (make-obsolete 'screen-height 'frame-height)
  501. (make-obsolete 'screen-width  'frame-width)
  502. (make-obsolete 'set-screen-width 'set-frame-width)
  503. (make-obsolete 'set-screen-height 'set-frame-height)
  504.  
  505.  
  506. ;;;; Key bindings
  507. (defvar ctl-x-5-map (make-sparse-keymap)
  508.   "Keymap for frame commands.")
  509. (defalias 'ctl-x-5-prefix ctl-x-5-map)
  510. (define-key ctl-x-map "5" 'ctl-x-5-prefix)
  511.  
  512. (define-key ctl-x-5-map "2" 'new-frame)
  513. (define-key ctl-x-5-map "0" 'delete-frame)
  514.  
  515. (provide 'frame)
  516.  
  517. ;;; frame.el ends here
  518.